package com.heartware.sh.blackhole.common;

import android.app.Fragment;

import com.heartware.sh.blackhole.entity.FragmentInfo;
import com.heartware.sh.blackhole.ui.fragment.MyFragment;
import com.heartware.sh.blackhole.ui.fragment.Null2Fragment;
import com.heartware.sh.blackhole.ui.fragment.NullFragment;
import com.heartware.sh.blackhole.ui.fragment.base.BaseNullFragment;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/22 0022.
 */

public class FragmentFactory {


    private static final String TITLE1 = "标题1";
    private static final String TITLE2 = "标题2";
    private static final String TITLE3 = "标题3";
    private static final String TITLE4 = "标题4";


    private static Map<Integer, FragmentInfo> map = new HashMap<>();

    /**
     * 创建FragmentInfo
     * @param i
     * @return
     */
    public static FragmentInfo createFragmentInfo(int i){

        FragmentInfo fi = map.get(i);


        if(fi == null){

            switch (i){
                case 0:
                    fi = new FragmentInfo(TITLE1, NullFragment.class);
                    break;

                case 1:
                    fi = new FragmentInfo(TITLE2, Null2Fragment.class);
                    break;

                case 2:
                    fi = new FragmentInfo(TITLE3, Null2Fragment.class);
                    break;

                case 3:
                    fi = new FragmentInfo(TITLE4, Null2Fragment.class);
                    break;
            }

            map.put(i, fi);

        }


        return fi;

    }


    private static Map<Integer, BaseNullFragment> mapFragment = new HashMap<>();

    public static BaseNullFragment createFragment(int i, FragmentInfo fragmentInfo){

        BaseNullFragment bnf = mapFragment.get(i);

        if(bnf == null){
            try {
                bnf = (BaseNullFragment) fragmentInfo.getFragment().newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            mapFragment.put(i, bnf);
        }

        return bnf;

    }


    /**
     * activity Two 使用的方法
     */
    static Map<Integer, FragmentInfo> actTwoMap = new HashMap<>();
    /**
     * 创建FragmentInfo
     * @param i
     * @param type
     * @return
     */
    public static FragmentInfo createFragmentInfo(int i, String type) {

        FragmentInfo fi = actTwoMap.get(i);

        if(fi == null){

            fi = new FragmentInfo(type, MyFragment.class);

            actTwoMap.put(i, fi);
        }

        return fi;

    }
}
